package com.myCode.thread;

/**
 * 如何中止线程的三种方法
 * 方法一：Stop() 方法
 * 方法二：interrupt()方法
 * 方法三：标志位法
 */
public class StopThreadDemo extends Thread{

    private volatile static boolean flag = true;
    @Override
    public void run() {
        // 方法三：标志位中止线程
        // FlagStopThread();

        interruptThread();

    }

    private void interruptThread() {
        System.out.println("interruptThread start run");

        int i = 0, j = 0;

    }

    private void FlagStopThread(){


        while (flag){
            System.out.println("运行中...........");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    public static boolean isFlag() {
        return flag;
    }

    public static void setFlag(boolean flag) {
        StopThreadDemo.flag = flag;
    }
}
